package com.test.dao.impl;

import com.test.dao.AccountDao;
import com.test.domain.Account;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.CallableStatementCallback;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.Types;
import java.util.List;

@Repository
public class AccountDaoImpl implements AccountDao {

    @Resource
    private JdbcTemplate jdbcTemplate;


    @Override
    public List<Account> getAccountListByCustomerPhoneNumber(String customerPhoneNumber) {
        String sql = "select * from account where customer_phone_number=?";

        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<Account>(Account.class),customerPhoneNumber);
    }

    @Override
    public int insertAccount(Account account) {


        Object execute_test = jdbcTemplate.execute("execute insert_account ?,?,?,?,? ",
                (CallableStatementCallback) cs -> {
                    cs.setObject("account_number",account.getAccountNumber());
                    cs.setObject("password",account.getPassword());
                    cs.setObject("type",account.getType());
                    cs.setObject("customer_phone_number",account.getCustomerPhoneNumber());
                    cs.setObject("status",-1);
                    cs.registerOutParameter("status", Types.INTEGER);
                    cs.execute();
                    Object ksbm = cs.getObject("status");
                    return ksbm;
                });
        System.out.println(execute_test);
        return (int)execute_test;

    }

    @Override
    public void deleteAccountByAccountNumberAndCustomerPhoneNumber(String accountNumber,String customerPhoneNumber) {
        String sql = "delete account where account_number=? and customer_phone_number=?";
        System.out.println(accountNumber);
        System.out.println(customerPhoneNumber);
        jdbcTemplate.update(sql,accountNumber,customerPhoneNumber);
    }
}
